import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

class MyEventHandler implements ActionListener
{
	private boolean listenerEnabled = true;
	private String mode;
        private String lastCoord;
	
	public MyEventHandler() {};
	
	public MyEventHandler(String mode)
	{
		this.mode = mode;
	}

	public void actionPerformed(ActionEvent event)
	{		
		System.out.println("Click!");
		
		if ((event.getSource() instanceof MyButton) && listenerEnabled == true) 
		{
			cellButtonClick((MyButton) event.getSource());
		}
		else
		{
			JButton btn = (JButton) event.getSource();
			controlButtonClick(btn);
		}
	}
	
	private void cellButtonClick(MyButton btn) 
	{
		listenerEnabled = false;
		Battle battle = Battle.getInstance();
		Control control = Control.getInstance();
		ArrayList<Area> areas = Battle.getInstance().getAreas();
		Timer timer = new Timer();
		AI ai = AI.getInstance();
		
		int[] coords = btn.getCoords();
		
		if (mode == "locating")
		{	
			Area area = areas.get(0);
			area.occupyCell(coords[0], coords[1]);
			System.out.println(area.cells[coords[0]][coords[1]]);
		}
		
		if (mode == "shooting")
		{
			System.out.println("Area " + battle.areaUnderAttack + " is under attack");
			Area area = areas.get(battle.areaUnderAttack);
			String coord = Tools.convertIntCoordsToString(coords[0], coords[1]);
			
			
			if (!Battle.isFinished())
			{		
				if (!area.coordIsShot(coord))
				{
					int result = area.shoot(coord);
                                        battle.stats.turn();
				
					System.out.println("The result is " + result);
				
					if (result == 0)
					{
						control.displayMessage("Missed");
						control.switchArea();
						btn.blue();
						
						if (!area.isAI() && ai.coordInMind() != null)
						{
							ai.success(false);
						}
						
					}
					else if (result == 1)
					{
						control.displayMessage("Got! Shoot one more time");
						btn.grey();
						
						if (!area.isAI())
						{
							System.out.println("Keeping a new coord: " + coord);
							ai.keepCoordInMind(coord);
							System.out.println("Coords in Mind: " + ai.coordsInMind());
						}
					}
					else if (result == -1)
					{
						control.displayMessage("Sunk! Shoot one more time");
						btn.grey();
						
						if (!area.isAI())
						{
							ai.keepCoordInMind(null);
							ai.success(true);
						}
						
						if (area.getShips().isEmpty())
						{
							System.out.println("The battle is finished");
							control.displayMessage("The battle is finished!");
							battle.setIsFinished(true);
                                                        battle.stats.stopTimer();
                                                        control.displayStats(battle.stats.getResults());
						}
						
						if (!area.isAI())
						{
							ai.keepCoordInMind(null);
							ai.success(false);
						}
					}
					
					Area areaUnderAttack = areas.get(battle.areaUnderAttack);
					
					if (!areaUnderAttack.isAI() && !Battle.isFinished())
					{
						System.out.println("simulating button click");
								
						timer.schedule(new TimerTask()
						{
							@Override
							public void run() 
							{
								control.simulateButtonClick();			
							}
						}, 1000);
					}
				}
				else
				{
					System.out.println("))))))))");
					btn.red();
				}
			}
			else
			{	
				control.displayMessage("The battle is finished");
			}
                        
                        
                        
		}
                
                
		listenerEnabled = true;
	}
	
	private void controlButtonClick(JButton btn)
	{	
		Control control = Control.getInstance();
		GUI gui = GUI.getInstance();
	
		if (mode == "prepareBattleField")
		{
			control.prepareBattle();
			control.messagePanel();
			control.shipsCountPanel();
			
			GUI.window.repaint();
		}
		
		if (mode == "locatingRandom")
		{
			Area area = Battle.getInstance().areas.get(0);
			area.locateShipsRandom();
			area.getMap().locateShips(area.getCells());
			area.getMap().enableLocatingMode();
		}
		
		if (mode == "startBattle")
		{
			int[] sufficiency = Battle.getInstance().areas.get(0).checkShipsSufficiency();
			
			boolean isSufficient = true;
			
			for (int i = 0; i < 3; i++)
			{
				if (sufficiency[i] == 0)
				{
					isSufficient = false;
					break;
				}
			}
			
			if (isSufficient)
			{
			    control.startBattle();
			}
			else
			{
				control.displayMessage("You must locate all of the required ships");
			}
		}
		
		if (mode == "menu")
		{
			control.menu();
		}
		/*
		btn.setVisible(false);
		Battle.getInstance().start();*/
	}
}